package com.dykj.fireFighting.server.monitoringCenter.service.impl;

import com.dykj.fireFighting.api.base.system.vo.SysDirectoryVo;
import com.dykj.fireFighting.api.monitoringCenter.entity.FireInformation;
import com.dykj.fireFighting.api.monitoringCenter.service.IFireInformationService;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.common.core.utils.UUIDUtils;
import com.dykj.fireFighting.common.core.vo.LoginUserVo;
import com.dykj.fireFighting.server.monitoringCenter.mapper.FireInformationMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 火灾记录
 *
 * @Author JacksonTu
 * @Date  2022-01-20 08:32:33
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class FireInformationServiceImpl extends BaseServiceImpl<FireInformationMapper, FireInformation> implements IFireInformationService {


    @Override
    public int importFire(Map<String, Object> params) {
        LoginUserVo user =(LoginUserVo) params.get("user");
        List<FireInformation> fireInformationList = (List<FireInformation>)params.get("fireInformationList");
        List<SysDirectoryVo> fireReason = (List<SysDirectoryVo>)params.get("fireReason");
        List<FireInformation> list = new ArrayList<>();
        //整理文件数据拼接关联字段relationId
        for (FireInformation d:fireInformationList) {
            if(StringUtils.isBlank(d.getAddress())){
                d.setAddress("未定义");
            }
            SysDirectoryVo fireReasonType=null;
            List<SysDirectoryVo> typeList=fireReason.stream().filter(sysDirectoryVo -> sysDirectoryVo.getValue().equals(d.getFireReason())).collect(Collectors.toList());
            if(typeList.size()>0){
                fireReasonType=typeList.get(0);
            }else{
                fireReasonType=fireReason.stream().filter(sysDirectoryVo -> sysDirectoryVo.getValue().equals("未定义")).collect(Collectors.toList()).get(0);
            }

            SysDirectoryVo regionType=null;
            List<SysDirectoryVo> typeList1=fireReason.stream().filter(sysDirectoryVo -> sysDirectoryVo.getValue().equals(d.getRegion())).collect(Collectors.toList());
            if(typeList1.size()>0){
                regionType=typeList1.get(0);
            }else{
                regionType=fireReason.stream().filter(sysDirectoryVo -> sysDirectoryVo.getValue().equals("未定义")).collect(Collectors.toList()).get(0);
            }

            SysDirectoryVo siteType=null;
            List<SysDirectoryVo> typeList2=fireReason.stream().filter(sysDirectoryVo -> sysDirectoryVo.getValue().equals(d.getSiteType())).collect(Collectors.toList());
            if(typeList2.size()>0){
                siteType=typeList2.get(0);
            }else{
                siteType=fireReason.stream().filter(sysDirectoryVo -> sysDirectoryVo.getValue().equals("未定义")).collect(Collectors.toList()).get(0);
            }

            d.setId(UUIDUtils.get32UUID());
            d.setCenterId(user.getEnterpriseId()+"");
            d.setFireReason(fireReasonType.getCode());
            d.setFireReasonText(fireReasonType.getValue());
            d.setRegion(regionType.getCode());
            d.setRegionText(regionType.getValue());
            d.setSiteType(siteType.getCode());
            d.setSiteTypeText(siteType.getValue());
            d.setCreateBy(user.getId()+"");
            list.add(d);
        }
        //拆分文件list
        List<List<FireInformation>> devList = splitListBycapacity(list, 5000);
        int count = 0;
        for (int i = 0; i < devList.size(); i++) {
            count += this.baseMapper.importFire(devList.get(i));
        }
        return count;
    }

    @Override
    public List<Map<String, Object>> getAllLnhz(Map<String, Object> map) {
        return this.baseMapper.getAllLnhz(map);
    }

    @Override
    public List<Map<String, Object>> getAllGq(Map<String, Object> map) {
        return this.baseMapper.getAllGq(map);
    }

    @Override
    public List<Map<String, Object>> getAllQhyy(Map<String, Object> map) {
        return this.baseMapper.getAllQhyy(map);
    }

    @Override
    public List<Map<String, Object>> getAllGcs(Map<String, Object> map) {
        return this.baseMapper.getAllGcs(map);
    }

    @Override
    public List<Map<String, Object>> getAllGsd(Map<String, Object> map) {
        return this.baseMapper.getAllGsd(map);
    }

    /**
     * 2> 根据目标容量 划分List
     *
     * @param source
     * @param capacity 划分完成的单个List容量
     * @param <T>
     * @return
     */
    private static <T> List<List<T>> splitListBycapacity(List<T> source, int capacity) {
        List<List<T>> result = new ArrayList<List<T>>();
        if (source != null){
            int size = source.size();
            if (size > 0 ){
                for (int i = 0; i < size;) {
                    List<T> value = null;
                    int end = i+capacity;
                    if (end > size){
                        end = size;
                    }
                    value = source.subList(i,end);
                    i = end;

                    result.add(value);
                }

            }else {
                result = null;
            }
        }else {
            result = null;
        }


        return result;
    }
}
